// packageA/pages/adminlist/index.js

Page({

    /**
     * 页面的初始数据
     */
    data: {
        accountList: [{
            id: 0,
            name: '谷总',
            password: '888888',
            roleId: 0,
            roleName: '苏总',
            roleCode: '超级管理员',
            createTime: '2025-09-20 14:00',
            status: true
          }],
        roleList: [
          { id: 1, name: '管理员', code: 'admin' },
          { id: 2, name: '分销分店', code: 'operator' },
          { id: 3, name: '配送员', code: 'viewer' }
        ],
        showModal: false,
        isEdit: false,
        currentAccount: {
          id: 0,
          name: '谷总',
          password: '888888',
          roleId: 0,
          roleName: '超级管理员',
          roleCode: '超级管理员',
          createTime: '2025-09-20 14:00',
          status: true
        },
        roleIndex: -1,
        searchText: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.loadAccountList();
    },
    loadAccountList() {
        const accountList = wx.getStorageSync('accountList') || [];
        if(accountList.length>0){
           this.setData({ accountList }); 
        }else{
            wx.setStorageSync('accountList', this.data.accountList);
        }
        
      },
    
      searchAccount(e) {
        const searchText = e.detail.value.toLowerCase();
        this.setData({ searchText });
        
        const allAccounts = wx.getStorageSync('accountList') || [];
        if (!searchText) {
          this.setData({ accountList: allAccounts });
          return;
        }
        
        const filteredAccounts = allAccounts.filter(account => 
          account.name.toLowerCase().includes(searchText) || 
          account.roleName.toLowerCase().includes(searchText)
        );
        this.setData({ accountList: filteredAccounts });
      },
    
      addAccount() {
        this.setData({
          showModal: true,
          isEdit: false,
          currentAccount: {
            id: 0,
            name: '',
            password: '',
            roleId: 0,
            roleName: '',
            roleCode: '',
            createTime: this.formatTime(new Date()),
            status: true
          },
          roleIndex: -1
        });
      },
    
      editAccount(e) {
        const id = e.currentTarget.dataset.id;
        const account = this.data.accountList.find(item => item.id == id);
        const roleIndex = this.data.roleList.findIndex(item => item.id == account.roleId);
        
        this.setData({
          showModal: true,
          isEdit: true,
          currentAccount: { ...account },
          roleIndex
        });
      },
    
      deleteAccount(e) {
        const id = e.currentTarget.dataset.id;
        wx.showModal({
          title: '提示',
          content: '确定要删除该账户吗？',
          success: (res) => {
            if (res.confirm) {
              const accountList = this.data.accountList.filter(item => item.id != id);
              wx.setStorageSync('accountList', accountList);
              this.setData({ accountList });
              wx.showToast({ title: '删除成功', icon: 'success' });
            }
          }
        });
      },
    
      closeModal() {
        this.setData({ showModal: false });
      },
    
      inputChange(e) {
        const field = e.currentTarget.dataset.field;
        const value = e.detail.value;
        this.setData({
          [`currentAccount.${field}`]: value
        });
      },
    
      roleChange(e) {
        const index = e.detail.value;
        const role = this.data.roleList[index];
        this.setData({
          roleIndex: index,
          [`currentAccount.roleId`]: role.id,
          [`currentAccount.roleName`]: role.name,
          [`currentAccount.roleCode`]: role.code
        });
      },
    
      statusChange(e) {
        this.setData({
          [`currentAccount.status`]: e.detail.value
        });
      },
    
      saveAccount() {
        const { currentAccount, isEdit, accountList } = this.data;
        
        if (!currentAccount.name || !currentAccount.password || !currentAccount.roleId) {
          wx.showToast({ title: '请填写完整信息', icon: 'none' });
          return;
        }
        
        let newAccountList = [...accountList];
        if (isEdit) {
          const index = newAccountList.findIndex(item => item.id == currentAccount.id);
          if (index !== -1) {
            newAccountList[index] = { ...currentAccount };
          }
        } else {
          const newId = newAccountList.length > 0 ? Math.max(...newAccountList.map(item => item.id)) + 1 : 1;
          newAccountList.push({
            ...currentAccount,
            id: newId,
            createTime: this.formatTime(new Date())
          });
        }
        
        wx.setStorageSync('accountList', newAccountList);
        this.setData({
          accountList: newAccountList,
          showModal: false
        });
        wx.showToast({ title: '保存成功', icon: 'success' });
      },
    
      formatTime(date) {
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const hour = date.getHours();
        const minute = date.getMinutes();
        
        return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day} ${hour < 10 ? '0' + hour : hour}:${minute < 10 ? '0' + minute : minute}`;
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})